package org.nativejpa.support.sqlgen;

/**
 * 表示一个SQL条件，用来构造 SQL 语句
 *
 * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
 * @create Jul 6, 2008 10:12:07 PM
 */
public class Condition {

    public static enum OPERATOR {
        GREATER(">"),
        GREATER_EQUAL(">="),
        EQUAL("="),
        NOT_EQUAL("!="),
        LESS("<"),
        LESS_EQUAL("<="),
        LIKE("LIKE");

        private String operator;
        OPERATOR(String operator) {
            this.operator = operator;
        }

        public String getOperator() {
            return operator;
        }

        public String toString() {
            return getOperator();
        }
    }

    public enum JointOperator {
        AND("AND"), OR("OR");

        private String operator;

        JointOperator(String operator) {
            this.operator = operator;
        }

        public String getOperator() {
            return operator;
        }

        public String toString() {
            return operator.toString();
        }
    }

    // db column name
    private String columnName;
    // operator < > = etc.
    private OPERATOR operator;

    protected Condition() {
    }

    public static Condition newCondition(String columnName, OPERATOR operator){
        return new Condition(columnName, operator);
    }

    Condition(String columnName, OPERATOR operator) {
        this.columnName = columnName;
        this.operator = operator;
    }

    public String getColumnName() {
        return columnName;
    }

    public OPERATOR getOperator() {
        return operator;
    }

    public Condition and(final Condition condition) {
        return new JointCondition(this, JointOperator.AND, condition);
    }

    public Condition or(final Condition condition) {
        return new JointCondition(this, JointOperator.OR, condition);
    }

    public String toString() {
        return getSQLTemplateString();
    }

    public String getSQLTemplateString() {
        return "(" + columnName + " " + operator.toString() + " $" + columnName + ")";
    }

    public static void main(String[] args) {
        System.out.println(new Condition("ID", OPERATOR.GREATER).or(new Condition("NAME", OPERATOR.EQUAL)).and(new Condition("age", OPERATOR.EQUAL)));
    }
}
